package cn.lin.task.dao;

import java.util.List;
import java.lang.reflect.ParameterizedType;
import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.lin.task.model.Pager;
import cn.lin.task.model.SystemContext;

public class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {
	
private Class<T> clz;
	
	@SuppressWarnings("unchecked")
	public Class<T> getClz() {
		if(clz==null) {
			//获取泛型的Class对象
			clz = ((Class<T>)
					(((ParameterizedType)(this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]));
		}
		return clz;
	}
	
	@Resource(name="sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
	
	public void add(T t) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(t);
	}

	public void delete(int id) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(this.load(id));
	}

	public T load(int id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(getClz(), id);
		
	}

	public void update(T t) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(t);
	}

	@SuppressWarnings("unchecked")
	public List<T> list(String hql,Object[] para) {
		// TODO Auto-generated method stub
		Query u = this.getSession().createQuery(hql);
		if(para!=null) {
			for(int i=0;i<para.length;i++) {
				u.setParameter(0, para[i]);
			}
		}
		List<T> list = u.list();
		return list;
	}

	public List<T> list(String hql, Object para) {
		return this.list(hql,new Object[]{para});
	}
	
	public List<T> list(String hql) {
		return this.list(hql,null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<T> find(String hql, Object[] args) {
		// TODO Auto-generated method stub
		Pager<T> pages=new Pager<>();
		int pageOffset=SystemContext.getPageOffset();
		int pageSize=SystemContext.getPageSize();
		Query query=this.getSession().createQuery(hql);
		Query query2=this.getSession().createQuery(getCountHql(hql));
		if(args!=null) {
			for(int i=0;i<args.length;i++) {
				query.setParameter(i, args[i]);
				query2.setParameter(i, args[i]);
			}
		}
	//	System.out.println("ef"+hql);
		//System.out.println(args);
		long totalRecord=(Long)query2.uniqueResult();
		query.setFirstResult(pageOffset);
		query.setMaxResults(pageSize);
		List<T> datas=query.list();
		//System.out.println("datas "+datas);
		pages.setDatas(datas);
		pages.setPageSize(pageSize);
		pages.setPageOffset(pageOffset);
		pages.setTotalRecord(totalRecord);
		return pages;
	}
	
	
	
		@SuppressWarnings("unused")
		private String getCountHql(String hql) {
			//1、获取from前面的字符串
			String f = hql.substring(0, hql.indexOf("from"));
			//2、将from前面的字符串替换为select count(*) 
			if(f.equals("")) {
				hql = "select count(*) "+hql;
			} else {
				hql = hql.replace(f, "select count(*) ");
			}
			//3、将fetch替换为""，因为抓取查询不能使用count(*)
			hql = hql.replace("fetch"," ");
			return hql;
		}

	@Override
	public Pager<T> find(String hql, Object agrs) {
		// TODO Auto-generated method stub
		return this.find(hql, new Object[] {agrs});
	}

	@Override
	public Pager<T> find(String hql) {
		// TODO Auto-generated method stub
		return this.find(hql,null);
	}

}
